home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
s-gikan2.maizuru-ct.ac.jp
/
s-gikan2.maizuru-ct.ac.jp.zip
/
s-gikan2.maizuru-ct.ac.jp
/
pub
/
old
/
ncvc351d_install.exe
/
{app}
/
scripts
/
transpose_laser_Gcode.pl
< prev
next >
Wrap
Perl Script
|
2007-10-23
|
5KB
|
222 lines
#! /usr/bin/perl
# âîü[âUü[ë┴ìHï@ùpé╠GâRü[âhé≡ò╧è╖é╖éΘâXâNâèâvâg #
# (G08, G11.1, G11 é¬æ╬Å█) #
# ÉΓæ╬ì└òWîn(G90)é╠é▌æ╬ë₧ #
use Math::Trig;
$EPSILON = 0.0001;
$pre_file= $ARGV[0];
$out_file= $ARGV[1];
open(IN,$pre_file);
@line = <IN>;
close(IN);
open(OUT,">$out_file");
my ($line,$i);
my ($G0X,$radius);
my $word;
my $commentLine;
my ($x,$y); #ê┌ô«æOé╠ì└òWé≡èiö[
my @abso; #ê┌ô«îπé╠ì└òW
my @pass; #Æ╩ë▀ô_é╠ì└òW
my @destination; #ê┌ô«îπé╠ì└òW
my @center; #ë~î╩é╠ÆåÉSì└òW
my @passAx; #ë~î╩òΓè╘Ä₧é╔é▄é╜é«Ä▓é╠âèâXâg
%XYZ = ("X",0,"Y",1,"Z",2,"I",0,"J",1);
for($i=0;$i<=$#line;$i++){
$_ = $line[$i];
if(/^\s*[\%]/){ print OUT; }
else{
($_, $commentLine) = divideComment($_);
if(/G0*?([018])[A-Z\s]/){ $G0X = $1; }
elsif(/G11[A-Z\s]/){ $G0X = 11; }
elsif(/G11.1[A-Z\s]/){ $G0X = 11.1; }
if($G0X == 8 and /D([^A-Z\s]+)/){
$radius = marume($1/2);
#î┤ò╢é≡âRâüâôâgÅoù═
outputOriginal($line[$i]);
#G08,D_,K_ê╚èO+âRâüâôâgé≡Åoù═
while(/[A-CE-JL-Z][^A-Z\s]+/g){
$word = $&;
if($word !~ /G0*8/){
print OUT $word;
}
}
print OUT $commentLine."\n";
$startX = marume($abso[0] + $radius);
$abso[0] = marume($abso[0] + $startX);
print OUT "G00X".$startX."\n";
print OUT "G03I-".$radius."\n";
}
elsif($G0X == 11){
($pass[0],$pass[1]) = ($abso[0],$abso[1]);
#î┤ò╢é≡âRâüâôâgÅoù═
outputOriginal($line[$i]);
while(/([XY])([^A-Z\s]+)/g){
$pass[$XYZ{$1}] = $2;
}
}
elsif($G0X == 11.1){
#î┤ò╢é≡âRâüâôâgÅoù═
outputOriginal($line[$i]);
($destination[0],$destination[1]) = ($abso[0],$abso[1]);
while(/([XY])([^A-Z\s]+)/g){
$destination[$XYZ{$1}] = $2;
}
($center[0],$center[1]) = getCenterFrom3P($abso[0],$abso[1],$pass[0],$pass[1],$destination[0],$destination[1]);
if($center[0] eq "error"){
print OUT "(-- ë~î╩òΓè╘é┼é½é▄é╣é± --)\n";
}
else{
$word = getG02orG03($abso[0],$abso[1],$destination[0],$destination[1],
$pass[0],$pass[1],$center[0],$center[1]);
$radius = getRadius($center[0]-$abso[0],$center[1]-$abso[1]);
print OUT $word."X".$destination[0]."Y".$destination[1].
"R".$radius."\n";
($abso[0],$abso[1]) = ($destination[0],$destination[1]);
}
}
else{
print OUT $line[$i];
while(/([XYZ])([^A-Z\s]+)/g){
$abso[$XYZ{$1}] = $2;
}
}
}
}
close(OUT);
sub marume{
my ($num) = @_;
if($coordinateNotation == 1){
return int($num+0.5);
}
else{
$num = int($num * 1000);
$num = $num / 1000;
if($num !~ /\./ and $num != 0){ $num = $num.'.'; }
return $num;
}
}
sub getCenterFrom3P{
my ($x1,$y1,$x2,$y2,$x3,$y3) = @_;
my ($A,$B,$C,$D,$E,$xc,$yc);
$E = ($x1-$x3)*($y1-$y2) - ($x1-$x2)*($y1-$y3);
if(abs($E) <= $EPSILON){
return "error";
}
else{
$A = ($y1-$y2)*($x1*$x1-$x3*$x3+$y1*$y1-$y3*$y3)/2;
$B = ($y1-$y3)*($x1*$x1-$x2*$x2+$y1*$y1-$y2*$y2)/2;
$C = ($x1-$x3)*($x1*$x1-$x2*$x2+$y1*$y1-$y2*$y2)/2;
$D = ($x1-$x2)*($x1*$x1-$x3*$x3+$y1*$y1-$y3*$y3)/2;
$xc = ($A-$B)/$E;
$yc = ($C-$D)/$E;
return ($xc,$yc);
}
}
sub getG02orG03{
my ($xs,$ys,$xe,$ye,$xp,$yp,$xc,$yc) = @_;
my ($As,$Ae,$Ap);
$As = getAngle($xs-$xc,$ys-$yc);
$Ae = getAngle($xe-$xc,$ye-$yc);
$Ap = getAngle($xp-$xc,$yp-$yc);
#CCWé⌐é╟éñé⌐é≡ö╗ÆΦé╖éΘ
#Æ╩ë▀ô_é¬èJÄnô_é╞ÅIù╣ô_é╠è╘é╠èpôxé╔éáéΩé╬üACCW
#èJÄnô_é¬ÅIù╣ô_éµéΦéαæσé½éóèpôxé╠é╞é½üACCWé╚éτé╬æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±éΘé═é╕
if($Ae < $As){
#æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±éΘé╞é½üAÅIù╣ô_é¬Æ╩ë▀ô_éµéΦéαæσé½éóèpôxé╚éτé╬üA
#Æ╩ë▀ô_é═æµ4Å█î└é⌐éτæµ1Å█î└é╔ë±é┴é╜îπé┼Æ╩ë▀é╖éΘé═é╕
if($Ap < $Ae){
$Ap += 2*pi;
}
$Ae += 2*pi;
}
if($As < $Ap and $Ap < $Ae){
return "G03";
}
else{
return "G02";
}
}
sub getAngle{
my ($x,$y) = @_;
my $theta = atan2($y,$x);
if($theta < 0){ $theta += 2*pi; }
return $theta;
}
sub getRadius{
my ($dx,$dy) = @_;
return marume(sqrt($dx*$dx+$dy*$dy));
}
sub divideComment{
my ($line) = @_;
my ($word, $newLine, $commentLine);
$newLine = "";
$commentLine = "";
# while( $line =~ /([A-Z][^A-Z\s\(]+|\s*\(.*?\)\s*)/g ){
while( $line =~ /([A-Z][^A-Z\s\(]+|\s*\(.*?\))/g ){
$word = $&;
if($word =~ /\(/){
$commentLine = $commentLine . $word;
}
else{
$newLine = $newLine . $word;
}
}
#if($commentLine eq ""){
# return $commentLine;
#}
#else{
return ($newLine,$commentLine);
#}
}
sub outputOriginal{
my ($line) = @_;
$line =~ s/\s*$//;
print OUT "(-- " .$line. " --)\n";
}